# -*- coding: utf-8 -*-
from re import sub

letters = {
        'а' : 'a',
        'б' : 'b',
        'в' : 'v',
        'г' : 'g',
        'д' : 'd',
        'е' : 'e',
        'ё' : '?',
        'ж' : 'zh',
        'з' : 'z',
        'и' : 'i',
        'й' : 'y',
        'к' : 'k',
        'л' : 'l',
        'м' : 'm',
        'н' : 'n',
        'о' : 'o',
        'п' : 'p',
        'р' : 'r',
        'с' : 's',
        'т' : 't',
        'х' : 'kh',
        'у' : 'u',
        'ф' : 'f',
        
        'ц' : 'ts',
        'ч' : 'ch',
        'ш' : 'sh',
        'щ' : 'sh',
        'ъ' : '?', 
        'ы' : 'y', 
        'ь' : '', 
        'э' : 'e',
        'ю' : 'yu',
        'я' : 'ya'
        
    }



def trim_all(text):     
    return sub('[\s\\\|&!]+', ' ', text)
    

def url_translit( text ):    
    
    text = lower_case( trim_all(text) )
    text = translit(text) 
    newText = []; n = newText.append
    for letter in text:
        if letter in letters.values() or letter == ' ':           
            n( letter  )        
    text = ''.join(newText)  
     
    text = text.replace(' ', '_')       
    
    return text
    



def translit( text ):    
    for letter, repl in letters.items(): 
        text = text.replace(letter, repl)
    return text




def upper_case(txt): 
    return txt.decode('utf-8').upper().encode('utf-8')


def lower_case(txt): 
    return txt.decode('utf-8').lower().encode('utf-8')


def capitalize_letters(text):
    if text:
        if len(text) > 1:
            new = text.decode('utf-8')
            return (new[0].upper()+ new[1:]).encode('utf-8')
        else:
            return text.upper()



